# -*- coding = utf-8 -*-
# @Time : 2021/11/2 0:03
# @Author : JingTian
# @file : 1.py
# @software : PyCharm

from bs4 import BeautifulSoup  # 网页解析，获取数据
import re  # 正则表达式，进行文字匹配
import urllib.request
import urllib.error  # 制定URL，获取网页数据
import xlwt  # 进行Excel操作
import sqlite3  # 进行SQLite数据库操作


def main():
    baseurl = "https://shenzhen.taoche.com/bmw/"
    # 爬取网页
    data_list = get_date(baseurl)
    save_path = ".\\ 深圳二手车.xls"
    # 保存数据
    # save_data(save_path)

    # ask_url("https://shenzhen.taoche.com/bmw/")


def get_date(baseurl):
    data_list = []
    for i in range(0, 1):
        url = baseurl + str(i*48)
        html = ask_url(url)

    # 逐一解析数据
    return data_list


# 得到指定一个url的网页内容
def ask_url(url):
    head = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.40"
    }
    request = urllib.request.Request(url, headers=head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
        print(html)
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)

    return html


def save_data(save_path):
    pass


if __name__ == "__main__":
    main()





















































